package com.xmy.dao.Impl;

import com.xmy.dao.BaseDao;
import com.xmy.dao.PetStoreDao;
import com.xmy.entity.Pet;
import com.xmy.entity.PetStore;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PetStoreDaoImpl extends BaseDao implements PetStoreDao {

    @Override
    public List<PetStore> SelPetStore() {
        List<PetStore> stores = new ArrayList<PetStore>();
        String sql = "select sid,loginid from petstore";
        ResultSet rs = executeQuery(sql,null);
        try{
            while (rs.next()){
                PetStore store = new PetStore();
                store.setSid(rs.getInt(1));
                store.setLoginid(rs.getString(2));
                stores.add(store);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return stores;
    }

    @Override
    public PetStore SelLogin(String loginid,String pwd) {
        String sql = "select loginid,balance,count(1) from petstore where loginid = ? and pwd = ?";
        Object [] objects = {loginid,pwd};
        PetStore petStore = null;
        ResultSet rs = null;
        int count = 0;
        try{
            rs = executeQuery(sql,objects);
            while (rs.next()){
                petStore = new PetStore();
                petStore.setLoginid(rs.getString(1));
                petStore.setBalance(rs.getInt(2));
                count = rs.getInt(3);
            }
            if (count == 0){
                petStore = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return petStore;
    }

    @Override
    public List<Pet> SelPetByBreed(String breed) {
        List<Pet> pets = new ArrayList<Pet>();
        ResultSet rs = null;
        String sql = "select petid,name,type,price from pet where breed = ?";
        try{
            rs = executeQuery(sql,new Object[]{breed});
            while (rs.next()){
                Pet pet = new Pet();
                pet.setPetid(rs.getInt(1));
                pet.setName(rs.getString(2));
                pet.setType(rs.getString(3));
                pet.setPrice(rs.getInt(4));
                pets.add(pet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pets;
    }
}
